/**
 * Created by Adi(adi@imeth.cn) on 2018/8/28.
 */
import { queryAuthResource } from '../services/auth';
import { reloadAuthorized } from '../utils/Authorized';
import { setAuthority } from '../utils/authority';

export default {
  namespace: 'auth',

  state: {
    authResources: [],
  },

  effects: {
    *fetchAuthResource(_, { call, put }) {
      const data = yield call(queryAuthResource);
      yield put({
        type: 'saveAuthResources',
        payload: data,
      });
    },
  },

  reducers: {
    saveAuthResources(state, { payload }) {
      setAuthority(payload.result);
      reloadAuthorized();
      return {
        ...state,
        authResources: payload,
      };
    },
  },
};
